package com.cll.netty.netty.dubborpc.netty;

import com.cll.netty.netty.dubborpc.provider.HelloServiceImpl;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

// 服务器这边handler比较简单
public class NettyServerHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            System.out.println("msg=" + msg);
            if (msg.toString().startsWith("HelloService#hello#")) {
                String result = new HelloServiceImpl().hello(
                        msg.toString().substring(
                                msg.toString().lastIndexOf("#") + 1
                        ));
                ctx.writeAndFlush(result);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
